/** 
 * Project Name:dyrj-admin
 * File Name:LoginController.java 
 * Package Name:com.dyrj.base.system.login.controller
 * Date:2017年10月6日上午12:28:08 
 * Copyright (c) 2017, zhang.xiangyu@foxmail.com All Rights Reserved. 
 */  
package com.dyrj.base.system.user.controller;

import javax.annotation.Resource;

import com.dyrj.base.system.org.service.TDepartmentService;
import com.dyrj.base.system.user.dao.SysUserMapper;
import com.dyrj.base.system.user.params.SysUserParams;
import com.dyrj.common.BaseController;
import com.dyrj.vo.PageResultMsg;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import com.dyrj.base.system.role.service.RoleService;
import com.dyrj.base.system.user.model.SysUser;
import com.dyrj.base.system.user.service.SysUserService;
import com.dyrj.vo.ResultMsg;

import java.util.List;

/**
 * @ClassName: UserController 
 * @Description: TODO(用户管理) 
 * @author
 * @date 2017年10月6日 上午2:52:14
 */
@Controller
@RequestMapping("/system/user")
public class SysUserController extends BaseController {
	@Resource
	private SysUserService service;
	@Resource
	private RoleService roleService;
	@Resource
	private TDepartmentService tDepartmentService;
	@Resource
	private SysUserMapper mapper;

	/**
	 * @Title: index 
	 * @Description: TODO(用户管理首页) 
	 * @param @return    设定文件 
	 * @return String    返回类型 
	 * @throws
	 */
	@GetMapping("/index")
	@RequiresPermissions("system:user:index")
	public String index(ModelMap map) {
//		if("0".equals(getUserId())){
//			map.addAttribute("dept", "nodept");
//		}else{
//			map.addAttribute("dept", "dodept");
//		}
		Long tenantid = getTenantid();
		List list = tDepartmentService.selectAllTreeDept(tenantid);
		map.addAttribute("deptMenuTree", list);
		map.addAttribute("allRoles", roleService.findRoles());
		return "system/user/user";
	}
	/**
	 * @Title: table 
	 * @Description: TODO(表格数据) 
	 * @return PageInfo<SysUser>    返回类型
	 */
	@GetMapping("/table")
	@ResponseBody
	@RequiresPermissions("system:user:index")
	public PageResultMsg table(SysUser record) {
	    record.setCreatoraccountid(getUserId());
		record.setTenantid(getTenantid());
		return service.table(record);
	}
	
	/**
	 * @Title: add 
	 * @Description: TODO(新增用户) 
	 * @param record
	 * @return ResultMsg    返回类型
	 */
	@PostMapping("/")
	@ResponseBody
	@RequiresPermissions("system:user:add")
	public ResultMsg add(SysUser record, @RequestParam(value = "roles[]", defaultValue = "-1") Long[] roles) {
		return service.add(record, roles);
	}
	
	/**
	 * @Title: info 
	 * @Description: TODO(详情) 
	 * @param id
	 * @return SysUser    返回类型
	 */
	@GetMapping("/{id}")
	@ResponseBody
	@RequiresPermissions("system:user:info")
	public SysUser info(@PathVariable Long id) {
		return service.info(id);
	}
	
	/**
	 * @Title: update 
	 * @Description: TODO(更新) 
	 * @param record
	 * @return ResultMsg    返回类型
	 */
	@PutMapping("/")
	@ResponseBody
	@RequiresPermissions("system:user:edit")
	public ResultMsg update(SysUser record, @RequestParam( value = "roles[]", defaultValue = "-1") Long[] roles) {
		return service.update(record, roles);
	}
	
	/**
	 * @Title: delete 
	 * @Description: TODO(删除) 
	 * @param id
	 * @return ResultMsg    返回类型
	 */
	@DeleteMapping("/{id}")
	@ResponseBody
	@RequiresPermissions("system:user:delete")
	public ResultMsg delete(@PathVariable Long id) {
		return service.delete(id);
	}

	/**
	 * @Title: changePsdIndex
	 * @Description: todo(修改密码主页)
	 * @Params: []
	 * @Return: java.lang.String    返回类型
	 * @Author:
	 * @Date: 2018/8/30 下午4:47
	 */
	@GetMapping("/changepsdindex")
	public String changePsdIndex() {
		return "system/user/changepsd";
	}

	/**
	 * @Title: changePassword
	 * @Description: todo()
	 * @Params: [oldPsw: 旧密码, newPsw: 新密码]
	 * @Return: com.dyrj.vo.ResultMsg    返回类型
	 * @Author:
	 * @Date: 2018/8/30 下午4:12
	 */
	@PutMapping("/changepsd")
	@ResponseBody
	public ResultMsg changePsd(SysUserParams record) {
		return service.changePassword(record);
	}

	@GetMapping("/list/{id}")
    @ResponseBody
    @RequiresPermissions("system:user:index")
    public ResultMsg list(@PathVariable Long id) {
        return service.selectUserByDeptId(id);
    }
	@GetMapping("/userInfo")
	@ResponseBody
	@RequiresPermissions("system:user:index")
	public SysUser userInfo() {
		return mapper.selectByPrimaryKey(getUserId());
	}

}
